const mongoose = require('mongoose');

const foodSchema = new mongoose.Schema({
  // 基本信息
  name: {
    type: String,
    required: true,
    trim: true
  },
  description: {
    type: String,
    required: true
  },
  category: {
    type: String,
    required: true,
    enum: ['川菜', '粤菜', '鲁菜', '苏菜', '浙菜', '闽菜', '湘菜', '徽菜', '火锅', '烧烤', '小吃', '甜品', '饮品', '西餐', '日料', '韩料', '其他']
  },
  
  // 价格信息
  price: {
    type: Number,
    required: true,
    min: 0
  },
  originalPrice: {
    type: Number,
    default: function() { return this.price; }
  },
  currency: {
    type: String,
    default: '¥'
  },
  
  // 图片信息
  images: [{
    type: String,
    required: true
  }],
  mainImage: {
    type: String,
    required: true
  },
  
  // 餐厅信息
  restaurant: {
    name: {
      type: String,
      required: true
    },
    address: {
      type: String,
      required: true
    },
    phone: {
      type: String,
      required: true
    },
    coordinates: {
      latitude: {
        type: Number,
        required: true
      },
      longitude: {
        type: Number,
        required: true
      }
    },
    businessHours: {
      type: String,
      default: '10:00-22:00'
    }
  },
  
  // 评价信息
  rating: {
    type: Number,
    required: true,
    min: 0,
    max: 5,
    default: 5.0
  },
  reviewCount: {
    type: Number,
    default: 0,
    min: 0
  },
  reviews: [{
    userId: {
      type: String,
      required: true
    },
    userName: {
      type: String,
      required: true
    },
    userAvatar: String,
    rating: {
      type: Number,
      required: true,
      min: 1,
      max: 5
    },
    content: {
      type: String,
      required: true
    },
    images: [String],
    createdAt: {
      type: Date,
      default: Date.now
    },
    helpful: {
      type: Number,
      default: 0
    }
  }],
  
  // 推荐标签
  tags: [{
    type: String
  }],
  
  // 特色信息
  features: [{
    type: String,
    enum: ['招牌菜', '网红推荐', '老字号', '新品上市', '限时特惠', '健康轻食', '素食友好', '儿童友好', '情侣约会', '商务宴请']
  }],
  
  // 营养信息（可选）
  nutrition: {
    calories: Number,
    protein: Number,
    carbs: Number,
    fat: Number,
    fiber: Number
  },
  
  // 配料信息
  ingredients: [String],
  
  // 口味信息
  taste: {
    spicy: {
      type: Number,
      min: 0,
      max: 5,
      default: 0
    },
    sweet: {
      type: Number,
      min: 0,
      max: 5,
      default: 0
    },
    sour: {
      type: Number,
      min: 0,
      max: 5,
      default: 0
    },
    salty: {
      type: Number,
      min: 0,
      max: 5,
      default: 0
    }
  },
  
  // 销售统计
  salesCount: {
    type: Number,
    default: 0,
    min: 0
  },
  monthlyOrders: {
    type: Number,
    default: 0,
    min: 0
  },
  
  // 状态信息
  isAvailable: {
    type: Boolean,
    default: true
  },
  isRecommended: {
    type: Boolean,
    default: false
  },
  isHot: {
    type: Boolean,
    default: false
  },
  
  // 优惠信息
  discount: String,
  promotionEndDate: Date
}, {
  timestamps: true
});

// 索引
foodSchema.index({ category: 1, rating: -1 });
foodSchema.index({ 'restaurant.address': 1 });
foodSchema.index({ rating: -1, salesCount: -1 });
foodSchema.index({ price: 1 });

// 静态方法
foodSchema.statics.getPopularFoods = function(limit = 10) {
  return this.find({ isAvailable: true })
    .sort({ salesCount: -1, rating: -1 })
    .limit(limit);
};

foodSchema.statics.getFoodsByCategory = function(category, limit = 20) {
  return this.find({ category, isAvailable: true })
    .sort({ rating: -1, salesCount: -1 })
    .limit(limit);
};

foodSchema.statics.searchFoods = function(keyword, limit = 20) {
  return this.find({
    isAvailable: true,
    $or: [
      { name: { $regex: keyword, $options: 'i' } },
      { description: { $regex: keyword, $options: 'i' } },
      { 'restaurant.name': { $regex: keyword, $options: 'i' } },
      { tags: { $in: [new RegExp(keyword, 'i')] } }
    ]
  })
  .sort({ rating: -1, salesCount: -1 })
  .limit(limit);
};

// 实例方法
foodSchema.methods.addReview = function(review) {
  this.reviews.push(review);
  this.reviewCount = this.reviews.length;
  
  // 重新计算平均评分
  const totalRating = this.reviews.reduce((sum, r) => sum + r.rating, 0);
  this.rating = Number((totalRating / this.reviews.length).toFixed(1));
  
  return this.save();
};

foodSchema.methods.incrementSales = function(quantity = 1) {
  this.salesCount += quantity;
  this.monthlyOrders += quantity;
  return this.save();
};

module.exports = mongoose.model('Food', foodSchema); 